package com.ewanghuiju.app.presenter.home;

import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.home.SearchShopContract;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.local.SearchHotBean;
import com.ewanghuiju.app.model.bean.response.HistorySearchResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.MyHttpResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

/**
 * @author sj  qq:751943509
 * @class 搜索
 * @time 2020/2/16
 */
public class SearchShopPresenter extends RxPresenter<SearchShopContract.View> implements SearchShopContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public SearchShopPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getHotdata(String request) {

    }

    @Override
    public void getHotWords() {
        addSubscribe(mDataManager.ptihotWords()
                .compose(RxUtil.<MyHttpResponse<List<SearchHotBean>>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<SearchHotBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<SearchHotBean>> listOptional) {
                        if (mView != null)
                            mView.showtHotWords(listOptional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }

    @Override
    public void getHistorydata() {
        addSubscribe(mDataManager.ptihistory()
                .compose(RxUtil.<MyHttpResponse<List<HistorySearchResponBean>>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<List<HistorySearchResponBean>>>(mView) {
                    @Override
                    public void onNext(Optional<List<HistorySearchResponBean>> listOptional) {
                        if (mView != null)
                            mView.showtHistorydata(listOptional.getIncludeNull());
                        //获取热门搜索
                        getHotWords();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void deleteHistorydata(String type, String name) {
        addSubscribe(mDataManager.ptihistoryDelete(type, name)
                .compose(RxUtil.<MyHttpResponse<BaseResponse>>rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> listOptional) {
                        if (mView != null)
                            mView.deleteHistorySuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mView != null)
                            mView.deleteHistoryFail();
                    }
                })
        );
    }
}
